import Promise from '../../../utils/es6-promise/es6-promise'
import regeneratorRuntime from '../../../utils/regenerator-runtime/runtime'
//var WxParse = require('../../../utils/wxParse/wxParse.js');
const utils = require('../../../utils/util.js');
const app=getApp();
Page({
  data: {
    bankid:null,
    bankname:null,
    url:"/api/Bank/getBankDetail",
    info:null,
    token:null,
    loading:true,
    jumpUrl:"../submit/index",
    iconSize: [20],
    iconColor: [
      '#f7625f', '#0aba05'
    ],
    iconType: [
      'success', 'cancel'
    ]
  },
  onLoad: async function (params) {
    console.log("onLoad");
    var id=params.id;
    var name=params.name;
    this.setData({
      bankid:id,
      bankname:name
    })
    wx.setNavigationBarTitle({
      title:name+"信用卡"
    });
    wx.showLoading({
      title:"加载中..."
    })
    const token = await app.getToken();
    this.setData({
      token:token
    })
    var result=await this._loadData();
    if(result.state){
      var data=result.info;
      this.setData({
        info:data
      })
      /**
      * WxParse.wxParse(bindName , type, data, target,imagePadding)
      * 1.bindName绑定的数据名(必填)
      * 2.type可以为html或者md(必填)
      * 3.data为传入的具体数据(必填)
      * 4.target为Page对象,一般为this(必填)
      * 5.imagePadding为当图片自适应是左右的单一padding(默认为0,可选)
      */
      /* var article = this.data.info.content;
      WxParse.wxParse('article', 'html', article, this,2); */
      var article = this.data.info.content;
      //article=article.replace(/<p>/g,'<view class="p">');
      //article=article.repalce(/src="/g,'src="'+app.globalData.baseUrl);
      article=this.escape2Html(article);
      //console.log(article);
      article=article.replace(/\<img src=\"/g,'<img class="image" src="'+app.globalData.basePath);
      article=article.replace(/\<p>/g,'<p class="p">');
      //console.log(article);
      this.setData({
        article:article,
        loading:false
      })
      wx.hideLoading();
    }
    else{
      this.setData({
        loading:false
      })
      wx.showToast({
        title:result.msg,
        icon:"none",
        duration:1000
      })
      wx.hideLoading();
    }
  },
  escape2Html: function (str) {
    var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; });
  },
  _loadData:function(){
    var p = new Promise((resolve,reject)=>{
        //做一些异步操作
        app.request(this.data.url,{token:this.data.token,bankid:this.data.bankid},res=>{
          //console.log(res);
          var data=res.data;
          /* var data={
            "state": true,
            "data": {
                "bankid": 1,
                "bankname": "中信银行",
                "content": "&lt;p&gt;一张信用卡&lt;/p&gt;"
            }
        }; */
          if(data.state){
            //resolve({state:true,info:JSON.parse(data.info)});
            resolve({state:true,info:data.data});
          }
          else{
            resolve({state:false,msg:data.msg});          
          }
        },error=>{
          resolve({state:false,msg:"刷新失败，请重试"});
        })
    });
    return p;
  },
  jumpSubmit:function(e){
    wx.navigateTo({
      url: this.data.jumpUrl+"?bankid="+this.data.bankid,
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  }
})